package com.nanuminc.market.utils;

import static com.nanuminc.market.utils.StringUtil.EMPTY;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

public class DomParserUtil {
	public Element getDomElement(String xml){
        Element order = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dom_builder;
        try {
            dom_builder = factory.newDocumentBuilder();
            InputStream istream = new ByteArrayInputStream(xml.getBytes("utf-8"));
            Document doc = dom_builder.parse(istream);
            order = doc.getDocumentElement(); 
        } catch (Exception e) {
            Log.d("nanum","Exception: "+e.getMessage().toString());
            e.printStackTrace();
        }
        return order;
    }
	
	public NodeList getNodeList(Element order, String value){
		return order.getElementsByTagName(value);
	}
		 
	public String getNode (int index, NodeList node ){
		StrReplace strrep = new StrReplace();
		String itemNode = EMPTY;
		  
		if (node != null && node.getLength() > 0) {
			Node item = node.item(index);
			Node text = item.getFirstChild();
            itemNode = text.getNodeValue();
            strrep.repStr(itemNode);
		} else {
            System.exit(1);
		}  
		return strrep.ConvStr;
	}
}
